#分解搜索
from typing import List

from langchain.chains import LLMChain
from langchain_core.output_parsers import PydanticOutputParser
from langchain_core.output_parsers.string import StrOutputParser
from langchain_core.output_parsers import ListOutputParser
from langchain.prompts import PromptTemplate,SystemMessagePromptTemplate,HumanMessagePromptTemplate,ChatPromptTemplate
from pydantic import BaseModel, Field
from langchain_community.chat_models import ChatOpenAI
from langchain_core.callbacks import StreamingStdOutCallbackHandler
chatllm=ChatOpenAI(streaming=True,verbose=True)

class LineListOutputParser(ListOutputParser):
    def parse(self, text: str) :
        lines = text.strip().split("\n")
        print(lines)
        return lines

systemMessage=SystemMessagePromptTemplate.from_template(template=""""接下来我会给你一个问题。我要你把它分解成一系列的子问题,最多分解成3个子问题。每个子问题都应该包含解决它所需的所有信息。
确保不要分解过多，也不要有任何无关紧要的子问题--我们会根据分解的简洁性、简明性和正确性来评估你。
example:
Question: What is Bitcoin?
What is the purpose of Bitcoin?
What does decentralized mean? """)
userMessage=HumanMessagePromptTemplate.from_template(template=""""{question}""",)
chat_prompt=ChatPromptTemplate.from_messages([systemMessage,userMessage])
# template=PromptTemplate(template=""
# Question: {question}\n""",input_variables=['question'])
deLLMChain=LLMChain(prompt=chat_prompt, output_parser=LineListOutputParser(),llm=chatllm,callbacks=[StreamingStdOutCallbackHandler()],verbose=True)
# for item in deLLMChain.stream({"question":"杭州旅游应该准备什么东西？"}):
#     print(item)